package com.we.connect.weconnect.controller.admin;

import com.we.connect.weconnect.model.common.ResultModel;
import com.we.connect.weconnect.model.system.SendUserInfo;
import com.we.connect.weconnect.model.system.SysUser;
import com.we.connect.weconnect.service.SendUserInfoService;
import com.we.connect.weconnect.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/admin")
public class SendUserInfoController {

    @Autowired
    private SendUserInfoService sendUserInfoService;

    @PostMapping("/addSendUserInfo")
    public String addSendUserInfo(@ModelAttribute("sendUserInfo") SendUserInfo sendUserInfo, Model model, HttpServletRequest request){
        SysUser loginSysUser = (SysUser) UserUtils.getSysUser(request, "loginSysUser");
        sendUserInfo.setUserId(loginSysUser.getUserId());
        Boolean isAddSuccess = sendUserInfoService.addSendUserInfo(sendUserInfo);
        if(isAddSuccess){
            return "redirect:/userInfo";
        }
        model = ResultModel.fail("新增失败", "列表", "/userInfo", model);
        return "common/tip";
    }

    @PostMapping("/updateSendUserInfo")
    public String updateSendUserInfo(@ModelAttribute("sendUserInfo") SendUserInfo sendUserInfo,
                                     Model model){
        Boolean isAddSuccess = sendUserInfoService.updateSendUserInfo(sendUserInfo);
        if(isAddSuccess){
            return "redirect:/userInfo";
        }
        model = ResultModel.fail("修改失败", "列表", "/userInfo", model);
        return "system/fail";
    }

    @RequestMapping("/delSendUserInfo/{id}")
    public String deleteSendUserInfo(@PathVariable("id") Long id, Model model){
        Boolean isAddSuccess = sendUserInfoService.deleteSendUserInfo(id);
        if(isAddSuccess){
            return "redirect:/userInfo";
        }
        model = ResultModel.fail("删除失败", "列表", "/userInfo", model);
        return "system/fail";
    }
}
